package com.chinaoct.siku.gzw.report;

import jdk.internal.jline.internal.Log;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class CsvUtils {

    public static List<String[]> readerCsv(String path) throws IOException {

        FileInputStream fis = new FileInputStream(path);
        InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
        BufferedReader reader = new BufferedReader(isr);
        CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT);
        //定义List用于接收文件数据
        List<String[]> list = new ArrayList<>();

        // 遍历CSV文件的每一行并获取单元格数据
        for (CSVRecord record : parser) {
            String[] singleRow = new String[record.size()];
            for (int i = 0; i < record.size(); i++) {
                singleRow[i]= record.get(i);
            }
            list.add(singleRow);
        }
        try{
            parser.close();
            reader.close();
            isr.close();
            fis.close();
        }catch (Exception e){
            Log.error(e.getMessage(),e);
        }

        return list;
    }

}
